import oss2

import uuid

def get_uuid():
    """
    Description:   生成uuid作为图片名，保证唯一性
    :return:       根据时间戳生成uuid
    """
    get_timestamp_uuid = uuid.uuid1()
    return get_timestamp_uuid

class OSSUtil:
    """用于阿里云OSS对象存储，上传和下载文件"""

    def __init__(self, ):
        self._auth = oss2.Auth('LTAI5tR8oNyLgJQu9b1XGRZA','7yh6T3iaDrDXWBrAHVjHOkZyW3y0w0')
        self._bucket = oss2.Bucket(self._auth, 'http://oss-cn-beijing.aliyuncs.com', 'compkey1')

    def put_object(self, image_path):
        """
        Description:   上传图片
        :param:
            filepath:   本地文件位置
        :return:       文件公网访问url
        """
        image_name = str(get_uuid()) + '.png'
        self._bucket.put_object_from_file("images/" + image_name, image_path)
        return "http://oss-cn-beijing.aliyuncs.com/images/" + image_name

    def put_md(self, md_path):
        """
        Description:   上传图片
        :param:
            filepath:   本地文件位置
        :return:       文件公网访问url
        """
        md_name = str(get_uuid()) + '.md'
        self._bucket.put_object_from_file("markdown/" + md_name, md_path)
        return "http://oss-cn-beijing.aliyuncs.com/md/" + md_name

if __name__ == '__main__':
    """Test"""
    _oss_util = OSSUtil()
    print(_oss_util.put_object(image_path="E:\Code\PythonProject\CompKey Algorithm System\static\images\Hello.jpg"))
